import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '../view/Login.vue';
import Home from '../view/Home.vue';
import Welcome from '../view/welcome.vue';
import Users from '../view/user/Users.vue';
import Rights from '../view/power/Rights.vue';
import Roles from '../view/power/Roles.vue';
import Cate from '../view/goods/Cate.vue';
import Params from '../view/goods/Params.vue';
import List from '../view/goods/List.vue';
import Order from '../view/order/Order.vue';
import AddGoods from '../view/goods/AddGoods.vue';
import Report from '../view/report/Report.vue';

Vue.use(VueRouter);

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/goods', component: List },
      { path: '/goods/add', component: AddGoods },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
      { path: '/params', component: Params }

    ]
  }
];

const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
export default router;
